﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using YXQ.Dao.ViewModel;
using AutoMapper;
namespace YXQ.Dao
{

    public class DADao
    {


        public DADao()
        {

            Mapper.CreateMap<Y_DA, DAModel>();
        }
        public List<Y_DA> GetList(int startIndex, int maxRow, out int recordcount)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                recordcount = edm.Y_DA.Count();

                return edm.Y_DA.OrderByDescending(s => s.Id).Skip(startIndex).Take(maxRow).ToList();

            }

        }


        public DAModel GetOneById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DA.FirstOrDefault(s => s.Id == id);
                return Mapper.Map<Y_DA, DAModel>(query);
            }

        }


        public int Save(DAModel obj)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DA.FirstOrDefault(s => s.Id == obj.Id);

                if (query == null)
                {


                    query = new Y_DA();

                    edm.AddToY_DA(query);

                }


                query.ProjectId = obj.ProjectId;

                query.ProjectTp = obj.ProjectTp;

                query.sjjs = obj.sjjs;

                query.dajd = obj.dajd;

                query.dajdother = obj.dajdother;

                query.jcys = obj.jcys;
                query.jcysother = obj.jcysother;

                query.ztys = obj.ztys;
                query.ztysother = obj.ztysother;

                query.dzzx = obj.dzzx;
                query.dzzxother = obj.dzzxother;

                query.wgys = obj.wgys;
                query.wgysother = obj.wgysother;

                query.yjjs = obj.yjjs;
                query.yjjsother = obj.yjjsother;

                query.cgrq = obj.cgrq;

                query.createuserid = obj.createuserid;

                query.createusername = obj.createusername;
                query.createrealname = obj.createrealname;
                query.createtime = obj.createtime;

                edm.SaveChanges();

                return query.Id;


            }

        }


        public DAModel GetOneByProjectId(int id, int reid)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_DA.Where(s => s.ProjectId == id&&s.Id==reid);
                

                return Mapper.Map<Y_DA, DAModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }
        public List<DAModel> GetByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_DA.Where(s => s.ProjectId == id);



                return Mapper.Map<List<Y_DA>, List<DAModel>>(query.OrderByDescending(s => s.Id).ToList());
            }
        }
        public DAModel GetOneByProjectId(int id)
        {
            using (YXQManageEntities edm = new YXQManageEntities())
            {
                var query = edm.Y_DA.Where(s => s.ProjectId == id);

                return Mapper.Map<Y_DA, DAModel>(query.OrderByDescending(s => s.Id).FirstOrDefault());
            }
        }


        public int DelById(int id)
        {


            using (YXQManageEntities edm = new YXQManageEntities())
            {

                var query = edm.Y_DA.FirstOrDefault(s => s.Id == id);

                if (query != null)
                {


                    edm.DeleteObject(query);


                }


                return edm.SaveChanges();


            }

        }


    }

}

